//
//  AppViewController.m
//  HGMMRAC
//
//  Created by eport on 2021/1/19.
//

#import "AppViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "AppViewModel.h"
#import "MBProgressHUD+NJ.h"
static NSString *iden_cell = @"iden_cell";


@interface AppViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;

@property(nonatomic,strong)AppViewModel *viewModel;

@end

@implementation AppViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self _initViews];
    [self _bindModels];
   
}

-(void)_bindModels{
    
    

    
    @weakify(self);
    
    [self.viewModel.loadCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {

        @strongify(self);
        [MBProgressHUD hideHUD];
        [self.tableview reloadData];
       
    }];

    [self.viewModel.loadCommand.errors subscribeNext:^(NSError * _Nullable x) {

        @strongify(self);
        [MBProgressHUD showError:x.domain];
        [MBProgressHUD hideHUD];
      
    }];

 

    
//    [MBProgressHUD showMessage:@"正在请求数据"];
//    
//    [self.viewModel.loadCommand execute:nil];
   
}

-(void)_initViews{
   self.title = @"应用商店";
    self.view.backgroundColor = [UIColor grayColor];
    
    [self _initTableviews];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(50, 100, 100, 60);
    button.backgroundColor = [UIColor blueColor];
    [button setTitle:@"点击" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(test) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:button];
    
    
    
   
}

-(void)test{
    
    [MBProgressHUD showMessage:@"正在请求数据"];
    [self.viewModel.loadCommand execute:nil];
}
   
-(void)_initTableviews{
        self.tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
    self.tableview.backgroundColor = [UIColor redColor];
        [self.view addSubview:self.tableview];
}
    
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden_cell];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:iden_cell];
        }
    
        AppModel *model = self.viewModel.dataArray[indexPath.row];
        cell.textLabel.text = model.name.length > 0 ?  model.name : @"jj";
        return cell;
        
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
      
       
}




-(AppViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[AppViewModel alloc]init];
    }
    return _viewModel;
}

@end
